<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Admin\Navigation;
use App\Models\Admin\Category;

class NavigationController extends Controller
{
    protected $fields = [
        'name'  => '',
        'url'  => '',
    ];
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Navigation $nav)
    {
        $navigation = $nav->orderby('sequence','DESC')->Paginate(15);
        return view('admin.navigation.index',compact('navigation'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = array();
        foreach($this->fields as $field => $default){
            $data[$field] = old($field,$default);
        }
        return view('admin/navigation/create',$data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,Navigation $nav)
    {
        foreach(array_keys($this->fields) as $field){
            $nav -> $field = $request->get($field);
        }
        $nav->save();
        event(new \App\Events\userActionEvent('\App\Models\Admin\Navigation', $nav->id, 1, '添加导航' . $nav->name));
        return redirect('/admin/navigation/index')->withSuccess('添加成功！');
    }

    /**
     *
     * set category into navigation
     */
    public function setnav(Navigation $nav,Category $cate,$id)
    {
        $category = $cate->find((int)$id);
        $navigation = $nav->where('name',$category->name)->get();
        if(!$navigation->isEmpty()){
            return redirect()->back()
                ->withErrors("该分类已设置为导航");
        }
        $nav->name = $category->name;
        $nav->url = '/category/'.$category->id;
        $nav->save();
        event(new \App\Events\userActionEvent('\App\Models\Admin\Navigation', $nav->id, 1,$nav->name.'设置为导航'));
        return redirect()->back()
            ->withSuccess("设置成功");
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Navigation $nav,$id)
    {
        $navigation = $nav->find((int)$id);
        return view('admin/navigation/edit',$navigation);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Navigation $nav,$id)
    {
        $navigation = $nav->find((int)$id);
        foreach(array_keys($this->fields) as $field){
            $navigation->$field = $request->get($field);
        }
        $navigation->save();
        event(new \App\Events\userActionEvent('\App\Models\Admin\Navigation', $navigation->id, 1, '添加导航' . $navigation->name));
        return redirect('/admin/navigation/index')->withSuccess('编辑成功！');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Navigation $nav,$id)
    {
        $navigation = $nav->find((int)$id);
        $navigation->delete();
        return redirect()->back()
            ->withSuccess("删除成功");
    }
}
